window.onload = function () {

//获取按钮
fun();
function fun(){
  var $myBnt = $('#i-footer div')
  var newImg = ['./indexResource/b1.png','./indexResource/b2.png','./indexResource/b3.png','./indexResource/b4.png','./indexResource/i2.png']
  var oldImg = ['./indexResource/1.png','./indexResource/2.png','./indexResource/3.png','./indexResource/4.png','./indexResource/i1.png']
  var myUrl = ['#/selected','#/finance','#/white','#/crowdFunding','#/mine']
  $($myBnt[0]).children('img').attr('src', newImg[0]);
  var newUrl = window.location.hash;                           //判断当前页面的window.location.hash;
  for (let i = 0; i < $myBnt.length; i++) {
    if(newUrl==myUrl[i]){
      for (var j = 0; j < newImg.length; j++) {
        $($myBnt[j]).children('img').attr('src', oldImg[j]);
      }
      $($myBnt[i]).children('img').attr('src', newImg[i]);
    }
    $myBnt[i].self = i;
    $myBnt[i].addEventListener('touchstart',function(){     //点击页面底部，切换图标图片
      for (var j = 0; j < newImg.length; j++) {
        $($myBnt[j]).children('img').attr('src', oldImg[j]);
        $('#m-last img').css('width', '30%');
      }
      if (i == 4) {
        $('#m-last img').css('width', '21%');
      }
      $($myBnt[this.self]).children('img').attr('src', newImg[this.self]);
      $('#indexBox').css('display',"none");
      $('#indexLeft').css('left',-$indexW);
      $('#i-header').css('left',0);
      $('#i-wrap').css('marginLeft',0);
    })
  }
  var $indexW = $('#indexLeft').width();    //侧栏部分
  $('#indexLeft').css('display',"block");
  $('#indexLeft').css('left',-$indexW);
  $('#i-login')[0].addEventListener('touchstart',function(){  //点击左上角登录或头像 弹出侧栏
    if(getCookie('username')=="" && window.location.hash=='#/login'){
    }else if(getCookie('username')=="" && window.location.hash=='#/mine'){
    }else{
      $('#indexBox').css('display','block')
      $('#indexLeft').animate({left:'0'},500)
      $('#i-header').animate({left:$indexW},500)
      $('#i-wrap').animate({marginLeft:$indexW},500)
    }
  })
  $('#indexBox')[0].addEventListener('touchstart',function(){   //点击页面，侧栏自动隐藏
    $('#indexBox').css('display',"none");
    $('#indexLeft').animate({left:-$indexW},500)
    $('#i-header').animate({left:0},500)
    $('#i-wrap').animate({marginLeft:0},500)
  })
  $('#l-main ul li')[0].addEventListener('touchstart',function(){   //点击请登录到登录界面
    window.location.hash = '#/mine';
    for (var j = 0; j < newImg.length; j++) {
      $($myBnt[j]).children('img').attr('src', oldImg[j]);
    }
    $('#indexBox').css('display',"none");
    $('#indexLeft').css('left',-$indexW);
    $('#i-header').css('left',0);
    $('#i-wrap').css('marginLeft',0);
    $($myBnt[4]).children('img').attr('src', newImg[4]);
  })
  if(getCookie('username')==""){                       //判断当前页面的Cookie
    $('#l-main ul li').html('请点击登录');
  }else{
    $('#l-main ul li').html('用户名：</br><span>'+getCookie('username')+'</span>');
  }
};
returnTop();                           // 返回顶部
function returnTop(){
  var top=document.querySelector('.backTop');
  window.onscroll=function(){
    var height=document.body.scrollTop;
      if(height>900){
        top.style.display='block';
      }else{
        top.style.display='none';
      }
    }
    top.addEventListener('touchstart',function(){
      document.body.scrollTop = 0;
    })
  };
  var u=getCookie('username');    //Cookie判断
  var p=getCookie('password');
  var login=document.querySelector('#i-login>a');
  if(u!='' && p!=''){
          login.innerHTML='';
      var img=document.createElement('img');
          img.style.width=0.5+'rem';
          img.style.height=0.5+'rem';
          login.appendChild(img);
          img.src='./mine/img/login.png';
  }
}
